#include "Shape.h"
#include <math.h>

#ifndef __CIRCLE_H_
#define __CIRCLE_H_

#define LINE_IT 1

class ENGINE_API Circle : public Shape
{
public:
	Circle() : Shape() 
	{
		#if (LINE_IT != 1)
		primitiveType = DrawPrimitives::TRIANGLE_STRIP;
		#else
		primitiveType = DrawPrimitives::LINE_STRIP;
		#endif
		setNumberOfFaces(30);
	}

	Circle(UINT faces) : Shape() 
	{
		#if (LINE_IT != 1)
		primitiveType = DrawPrimitives::TRIANGLE_STRIP;
		#else
		primitiveType = DrawPrimitives::LINE_STRIP;
		#endif
		setNumberOfFaces(faces);
	}

	void setNumberOfFaces(UINT faces)
	{
		DWORD color = 0xff000000;

		if (faces %2 != 0) faces++;
		if (faces > 144) faces = 144;
		
		vertexCount = faces + 2;

		if(vertexs)
		{
			color = vertexs[0].color;
			delete[] vertexs;
		}

		vertexs = new COL_VERTEX[vertexCount];
		if(vertexs == NULL) throw std::bad_alloc();

		setColor(color);
		resetVertexs();
	}

	void resetVertexs()
	{
		//* 3.14159f / 180.0f
		double dPiOver180 = 3.141592654f / 180.0;
		float hOv2 = Height/2.0f;//height over two
		float wOv2 = Width/2.0f;//width over two
		
		//rightmost
		vertexs[0].x = posX + Width;
		vertexs[0].y = posY + hOv2;
		vertexs[0].z = 1.0f;
		
		for(int i = 1; i * 2 < vertexCount; i++)
		{
			
			double dAngle = (360.0 / ((double)vertexCount/2)) * i;
			double dAngSin = sin(dAngle * dPiOver180);
			double dAngCos = cos(dAngle * dPiOver180);
			
			vertexs[(i * 2)-1].x = posX + wOv2 + wOv2 * dAngCos;
			vertexs[(i * 2)-1].y = posY + hOv2 - hOv2 * dAngSin;
			vertexs[(i * 2)-1].z = 1.0f;
			
			vertexs[(i * 2)].x = posX + wOv2 + wOv2 * dAngCos;
			vertexs[(i * 2)].y = posY + hOv2 + hOv2 * dAngSin;
			vertexs[(i * 2)].z = 1.0f;

		}
		
		//leftmost
		vertexs[vertexCount - 1].x = posX;
		vertexs[vertexCount - 1].y = posY + hOv2;
		vertexs[vertexCount - 1].z = 1.0f;
		
	}

	~Circle() { delete[] vertexs; }
	


};

#undef LINE_IT

#endif